function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } window.ctMaxUploadFiles = 1; var _ctAsciiState = { image: null, fileName: 'image' }; var _ctAsciiCharsets = { detailed: '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`. ', simple: '@%#*+=-:. ' }; function _ctAsciiRender() { var img = _ctAsciiState.image; var out = document.getElementById('box2'); if (!img) { if (out) out.value = ''; return; } var w = parseInt((document.getElementById('ascii-width') || {}).value, 10); if (!isFinite(w)) w = 100; w = Math.max(20, Math.min(300, w)); var widthLabel = document.getElementById('ascii-width-label'); if (widthLabel) widthLabel.textContent = w; var charsetKey = (document.getElementById('ascii-charset') || {}).value || 'detailed'; var chars = _ctAsciiCharsets[charsetKey] || _ctAsciiCharsets.detailed; var dark = !!(document.getElementById('ascii-dark') || {}).checked; if (dark) chars = chars.split('').reverse().join(''); var aspect = 0.5; // chars are about twice as tall as wide var h = Math.max(1, Math.round((img.naturalHeight / img.naturalWidth) * w * aspect)); var canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; var ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, w, h); var data; try { data = ctx.getImageData(0, 0, w, h).data; } catch (err) { alert('Could not read pixels from this image.'); return; } var lines = []; for (var y = 0; y < h; y++) { var line = ''; for (var x = 0; x < w; x++) { var i = (y * w + x) * 4; var r = data[i], g = data[i+1], b = data[i+2], a = data[i+3]; var lum; if (a === 0) lum = 1; else lum = (0.299*r + 0.587*g + 0.114*b) / 255; var idx = Math.min(chars.length - 1, Math.floor(lum * (chars.length - 1))); line += chars.charAt(idx); } lines.push(line); } var ascii = lines.join('\n'); if (out) { out.value = ascii; try { if (typeof writeCharacterAndWordCount === 'function') writeCharacterAndWordCount(); } catch (e) {} } var preview = document.getElementById('ascii-preview'); if (preview) preview.textContent = ascii; var base = (_ctAsciiState.fileName || 'image').replace(/\.[^.]+$/, ''); var anchor = document.getElementById('ascii-download'); if (anchor) { try { URL.revokeObjectURL(anchor.href); } catch (e) {} var blob = new Blob([ascii], { type: 'text/plain;charset=utf-8' }); anchor.href = URL.createObjectURL(blob); anchor.download = base + '.txt'; anchor.style.display = ''; } } function processFile(blob, fileName) { _ctAsciiState.fileName = fileName || 'image'; var img = new Image(); img.onload = function() { _ctAsciiState.image = img; var wrap = document.getElementById('ascii-controls'); if (wrap) wrap.style.display = ''; _ctAsciiRender(); }; img.onerror = function() { alert('Could not decode this image.'); }; img.src = URL.createObjectURL(blob); } $(function(){ $('#ascii-width, #ascii-charset, #ascii-dark').on('input change', _ctAsciiRender); }); var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }